package com.wandian.news.dialog;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mylib.libcore.tools.CSJADTools;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.news.R;

import static com.mylib.libcore.config.AppConfig.CSJ_BANNER_ONE_ID;


/**
 * 挑战弹框
 */
public class ChallengeResultDialog extends PopupWindow {

    private ImageView iv_close;
    private TextView tv_answer,tv_again,tv_result,tv_challenge_title,tv_reason;
    private View mMenuView;
    public ChallengeDialogBtnClink dialogViewClink;
    public interface  ChallengeDialogBtnClink{
        void viewBtnClink();
    }

    public ChallengeResultDialog(Context context, String title,String answer,String reason, ChallengeDialogBtnClink dialogViewClink) {
        super(context);
        this.dialogViewClink =dialogViewClink;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.news_dialog_challenge_result, null);
        iv_close = (ImageView)mMenuView.findViewById(R.id.iv_close);
        tv_answer = (TextView) mMenuView.findViewById(R.id.tv_answer);
        tv_again = (TextView) mMenuView.findViewById(R.id.tv_again);
        tv_result = (TextView) mMenuView.findViewById(R.id.tv_result);
        tv_reason = (TextView) mMenuView.findViewById(R.id.tv_reason);
        tv_challenge_title = (TextView) mMenuView.findViewById(R.id.tv_challenge_title);
        tv_challenge_title.setText(title+"");
        if(title != null && title.equals("挑战成功")){
            tv_answer.setVisibility(View.GONE);
            tv_result.setText("战力：+5");
            tv_result.setTextColor(context.getResources().getColor(R.color.text_green));
        }else {
            tv_answer.setVisibility(View.VISIBLE);
            tv_result.setText("战力：-5");
            tv_result.setTextColor(context.getResources().getColor(R.color.red));
        }
        if(!UIUtils.isEmpty(reason)){
            tv_reason.setVisibility(View.VISIBLE);
            tv_reason.setText("注解："+reason);
        }else {
            tv_reason.setVisibility(View.GONE);
        }
        tv_answer.setText("正确答案："+answer);
        iv_close.setOnClickListener(v -> dismiss());
        tv_again.setOnClickListener(v -> {
            dismiss();
            dialogViewClink.viewBtnClink();
        });

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            this.setClippingEnabled(false);
        }

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
//        //设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.popwin_anim_style);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
    }


    @Override
    public void showAsDropDown(View anchor) {
        if(Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        if(Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor, xoff, yoff);
    }

}